package com.lib.common.helper.arouter.mapping;

import android.text.TextUtils;

import com.google.gson.annotations.SerializedName;
import com.lib.framework.manager.LogManager;

import java.util.Collections;
import java.util.List;

/**
 * Created by Zhangjiaxing on 2019/1/4.
 * If there are any problems, please
 * send email to kevin.zhang@xiangwushuo.com
 *
 * @descrpition
 **/
public class PathMapping {
    public static final PathMapping EMPTY = new PathMapping();

    @SerializedName(value = "path-mapping", alternate = {"pathMapping"})
    private List<PathInfo> mPaths;

    public PathMapping() {
        mPaths = Collections.emptyList();
    }

    public List<PathInfo> getPaths() {
        return mPaths;
    }

    public void setPaths(List<PathInfo> paths) {
        mPaths = paths;
    }

    public PathInfo getPathInfoByCode(String code) {
        if (!TextUtils.isEmpty(code)) {
            for (PathInfo path : mPaths) {
                if (code.equals(path.getCode())) {
                    LogManager.i("PathMapping getPathInfoByCode", path.toString());
                    return path;
                }
            }
        }

        return null;
    }

    public PathInfo getPathInfoByPath(String path) {
        if (!TextUtils.isEmpty(path)) {
            for (PathInfo item : mPaths) {
                if (path.equals(item.getPath())) {
                    LogManager.i("PathMapping getPathInfoByPath", item.toString());
                    return item;
                }
            }
        }

        return PathInfo.EMPTY;
    }


    public String getCode(String path) {
        return getPathInfoByPath(path).getCode();
    }

    public String getPath(String code) {
        PathInfo pathInfo = getPathInfoByCode(code);
        if (pathInfo == null) {
            return "";
        }

        return pathInfo.getPath();
    }
}
